//体测信息模块
$(function () {
    /**
     * 体测评分
     */
    $('#manager-grade').datagrid({
        url: Think.APP + "/Nav/nav_menu", //数据源
        fit: true, //自适应
        striped: true,
        fitColumns: true,
        toolbar: '#grade-toolbar',
        border: false, //取消边框
        pagination: true, //分页开启
        pageSize: 50,
        rownumbers: true,
        columns: [[
                {title: 'ID', field: 'id', checkbox: true},
                {field: 'name', title: '菜单名', width: 100, align: 'center'},
                // {field: 'name_url', title: '菜单链接', width: 100, align: 'center'},
                {field: 'iconcls', title: '图标', width: 100, align: 'center'},
                {field: 'closed', title: '是否开启', width: 100, align: 'center'},
                {field: 'add_time', title: '添加时间', width: 100, align: 'center'},
                {field: 'action', title:'操作',  width:100, align:'center',formatter:function(value,rec){
                var btn = '<a class="hand_import_action" onclick="role_toolbar.select(\''+rec.id+'\',\''+rec.name+'\')" href="javascript:void(0)">查看子菜单</a>|'+
                          '<a class="auto_import_action" onclick="role_toolbar.add(\''+rec.id+'\',\''+rec.name+'\')" href="javascript:void(0)">添加子菜单</a>';
                return btn;
                }}
            ]],
            onClickRow: function(rowIndex, rowData) {//单击事件
                $('#manager-scope-import').datagrid("clearChecked"); //取消所有行的选中
                $('#manager-scope-import').datagrid("selectRow", rowIndex);  //选中当前行
            },
             onLoadSuccess:function(data){
               $('.hand_import_action').linkbutton({text:'查看子菜单',plain:true,iconCls:'icon-add-new'});
               $('.auto_import_action').linkbutton({text:'添加子菜单',plain:true,iconCls:'icon-add-new'});
           },
    });

    // $('#import-select-list').datagrid({
    //     url: Think.APP + "/Nav/nav_son_menu", //数据源
    //     fit: true, //自适应
    //     striped: true,
    //     fitColumns: true,
    //     border: false, //取消边框
    //     pagination: true, //分页开启
    //     pageSize: 50,
    //     rownumbers: true,
    //     onClickRow: function(rowIndex, rowData) {//单击事件
    //         $('#manager-scope-import').datagrid("clearChecked"); //取消所有行的选中
    //         $('#manager-scope-import').datagrid("selectRow", rowIndex);  //选中当前行
    //     },
    //      onLoadSuccess:function(data){
    //        if(data.total==0){
    //        alert("没有数据！");
    //        }
    //        $('.hand_import_action').linkbutton({text:'查看子菜单',plain:true,iconCls:'icon-add-new'});
    //        $('.auto_import_action').linkbutton({text:'添加子菜单',plain:true,iconCls:'icon-add-new'});
    //    },
    //     columns: [[
    //             {field: 'name', title: '菜单名', width: 100, align: 'center'},
    //             {field: 'name_url', title: '菜单链接', width: 100, align: 'center'},
    //             {field: 'closed', title: '是否开启', width: 50, align: 'center'},
    //             {field: 'add_time', title: '添加时间', width: 100, align: 'center'},
    //             {field: 'action', title:'操作',  width:100, align:'center',formatter:function(value,rec){
    //             var btn = '<a class="hand_import_action" onclick="role_toolbar.select_son(\''+rec.id+'\')"  href= "javascript:void(0)">查看子菜单</a>|'+
    //                       '<a class="auto_import_action" onclick="role_toolbar.add(\''+rec.id+'\')" href="javascript:void(0)">添加子菜单</a>';
    //             return btn;
    //             }}
    //         ]],
    //
    // });



    //显示添加框
    function add_menu_dialog(data,name){
    $('#import-add-form').dialog({
        title: name+' 添加子菜单',
        width: 500,
        closed: true,
        modal: true, //遮罩层
        buttons: [{
                text: "确 定",
                handler: function () {
                    //获取数据
                    $('#import-add-form').form({
                        url: Think.APP + "/Nav/add_menu?id="+data,
                        dataType: "json",
                        success: function (data) {
                          //  var data = eval('(' + data + ')');
                          var data = $.parseJSON(data);
                            if (data.success === 1) {
                               $.messager.alert('提示', data.msg, 'success');
                                //刷新
                                $('#import-add-form').dialog("close").form("reset");  //关闭窗体
                                $('#manager-scope-import').datagrid("reload");  //重新加载数据

                            } else
                            {
                                //添加失败
                                $.messager.alert('提示', data.msg, 'error');
                            }
                        }
                    });
                    //提交表单
                    $('#import-add-form').submit();
                }
            }, {
                text: "取 消",
                handler: function () {
                    //关闭
                    $('#import-add-form').dialog("close").form('reset');
                }
            }]
    });

      $('#import-add-form').dialog("open");//开启
}
    //导入工具条
    role_toolbar = {
        add: function (data,name) {  //添加
            //弹出dialog
            add_menu_dialog(data,name);
          
        },
        select:function(data,name){  //查看子菜单
          //弹出dialog

          open_dialog(data,name);
          get_menu_list(data);
        },
        select_son:function(data){  //查看子菜单
          //弹出dialog
          //console.log(data);
          get_menu_list(data);

        }
    }

    function open_dialog(data,name){
      //显示
      var id = data;
      $('#import-select-form').dialog({
          title: name+ "子菜单",
          width: 900,
          height:500,
          closed: true,
          modal: true, //遮罩层
          buttons: [{
                  text: "返回上一级",
                  handler: function () {
                  get_menu_list(id);

              }
            } , {
                  text: "取 消",
                  handler: function () {
                      //关闭
                      $('#import-select-form').dialog("close");
                  }

              }]
      });
      $('#import-select-form').dialog("open");
    }


    function get_menu_list(data){
      var id = data;
      $('#import-select-list').datagrid({
          url: Think.APP + "/Nav/nav_son_menu?id="+id, //数据源
          fit: true, //自适应
           striped: true,
          fitColumns: true,
           border: false, //取消边框
          pagination: true, //分页开启
          pageSize: 50,
          // rownumbers: true,
          onLoadSuccess:function(data){

              if(data.total==0){
              alert("没有数据！");
              }
            $('.hand_import_action').linkbutton({text:'查看子菜单',plain:true,iconCls:'icon-add-new'});
            $('.auto_import_action').linkbutton({text:'添加子菜单',plain:true,iconCls:'icon-add-new'});
        },
          columns: [[
                  {field: 'name', title: '菜单名', width: 100, align: 'center'},
                  {field: 'name_url', title: '菜单链接', width: 100, align: 'center'},
                  {field: 'closed', title: '是否开启', width: 50, align: 'center'},
                  {field: 'add_time', title: '添加时间', width: 100, align: 'center'},
                  {field: 'action', title:'操作',  width:100, align:'center',formatter:function(value,rec){
                  var btn = '<a class="hand_import_action" onclick="role_toolbar.select_son(\''+rec.id+'\',\''+rec.name+'\')"  href= "javascript:void(0)">查看子菜单</a>|'+
                            '<a class="auto_import_action" onclick="role_toolbar.add(\''+rec.id+'\',\''+rec.name+'\')" href="javascript:void(0)">添加子菜单</a>';
                  return btn;
                  }}
              ]],
      });
    }



});
